package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private ISysDictDataService sysDictDataService;
    /**
     * 新增商机跟进信息
     * @param businessTrackVo
     */
    @Override
    @Transactional
    public void addTbBusinessTrackRecord(BusinessTrackVo businessTrackVo) {
        //创建一个商机跟进表对象
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        //将请求参数中和商机跟进表有关的信息拷贝到商机跟进对象中
        BeanUtils.copyBeanProp(tbBusinessTrackRecord,businessTrackVo);
        //给商机跟进对象设置跟进人,和当前跟进时间
        String username = SecurityUtils.getUsername();
        tbBusinessTrackRecord.setCreateBy(username);

        Date nowDate = DateUtils.getNowDate();
        tbBusinessTrackRecord.setCreateTime(nowDate);
        //调用mapper层的方法将数据存储到商机跟进表中
        tbBusinessTrackRecordMapper.addTbBusinessTrackRecord(tbBusinessTrackRecord);
        //创建一个商机对象
        TbBusiness tbBusiness = new TbBusiness();
        //将请求参数中和商机有关的信息拷贝到商机对象中
        BeanUtils.copyBeanProp(tbBusiness,businessTrackVo);
        //通过businessId给商机设置id属性
        Long businessId = businessTrackVo.getBusinessId();
        tbBusiness.setId(businessId);
        //将商机的状态设置为2:跟进中
        tbBusiness.setStatus("2");
        //调用mapper层方法,根据id修改商机表中的数据
        tbBusinessMapper.updateTbBusiness(tbBusiness);
    }

    @Override
    public List<TbBusinessTrackRecord> findAllRecords(Long businessId) {
        //调用mapper层方法,查询结果
        List<TbBusinessTrackRecord> tbBusinessTrackRecords = tbBusinessTrackRecordMapper.findAllRecords(businessId);
        //获取集合中的key_items元素
        for (TbBusinessTrackRecord tbBusinessTrackRecord : tbBusinessTrackRecords) {
            String keyItems = tbBusinessTrackRecord.getKeyItems();
            //对keyItems根据","进行切割
            String[] keys = keyItems.split(",");
            //新建一个集合
            ArrayList<String> keyList  = new ArrayList<>();
            //遍历数组,将数组中的每一个元素通过字典查询出具体的沟通要点
            for (String key : keys) {
                String label = sysDictDataService.selectDictLabel("communication_point", key);
                //将值存入集合中
                keyList.add(label);
            }
            tbBusinessTrackRecord.setKeys(keyList);
        }
        return tbBusinessTrackRecords;
    }
}
